package action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections4.map.HashedMap;

import com.google.gson.Gson;


import pojo.BaseDataPojo;

import pojo.Message;
import pojo.MessageComment;
import pojo.User;
import service.MallService;
import service.MessageService;
import serviceImpl.MallServiceImpl;
import serviceImpl.MessageServiceImpl;




@WebServlet("/MessageServlet")
public class MessageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public MessageServlet() {
        super();
        
    }

	
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		doPost(req, resp);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String mark = request.getParameter("mark");
		switch(mark) {
		
		
		case "getAllMessage":   // 获取所有考研热点组成的ArrayList,即ArrayList<Message>
			getAllMessage(request,response);
			break;
		case "getSingleMessage":  // 根据mid获取单个Message
			getSingleMessage(request,response);
			break;
		case "getUserInfo":  // 根据userid获取单个user
			getUserInfo(request,response);
			break;
		case "getMessageComment":  // 根据mid获取用户评论
			getMessageComment(request,response);
			break;
		case "sendMcComment":  // 根据mid获取用户评论
			sendMcComment(request,response);
			break;
		}
	}
	
	
	private void sendMcComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		int userid = Integer.parseInt(request.getParameter("userid"));
		int mid = Integer.parseInt(request.getParameter("mid"));
		String mcComment = request.getParameter("mcComment");
		
		MessageService ms = new MessageServiceImpl();
		int suc = 0;
		suc = ms.sendMcComment(mid,userid,mcComment);
		Map<String,Integer> map = new HashedMap<>();
		map.put("suc",suc);
		System.out.println("suc:"+suc);
		if(suc==1||suc==0) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Integer>>("为空或失败", false, map)));
		}
		
	}


	private void getMessageComment(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		int mid = Integer.parseInt(request.getParameter("mid"));
		MessageService ms = new MessageServiceImpl();
		ArrayList<MessageComment> mcList = new ArrayList<MessageComment>();
		mcList = ms.getMessageComment(mid);
		Map<String,ArrayList<MessageComment>> map = new HashedMap<>();
		map.put("mcList",mcList);
		if(mcList!=null) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<MessageComment>>>("根据mid获取单个Message成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<MessageComment>>>("根据mid获取单个Message为空或失败", false, map)));
		}
		
	}


	private void getUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		int userid = Integer.parseInt(request.getParameter("userid"));
		MessageService ms = new MessageServiceImpl();
		User user = new User();
		user = ms.getUserInfo(userid);
		Map<String,User> map = new HashedMap<>();
		map.put("user",user);
		if(user!=null) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					User>>("根据mid获取单个Message成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					User>>("根据mid获取单个Message为空或失败", false, map)));
		}
		
	}


	private void getAllMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 设置前后端交互参数的编码为utf-8		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();		
		
		MessageService ms = new MessageServiceImpl();
		ArrayList<Message> messageList = new ArrayList<Message>();
		messageList = ms.getAllMessage();
		System.out.println(messageList);
		Map<String,ArrayList<Message>> map = new HashedMap<>();
		map.put("messageList",messageList);
		if(messageList!=null) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<Message>>>("获取ArrayList<Message>成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					ArrayList<Message>>>("获取ArrayList<Message>为空或失败", false, map)));
		}
			
	}
	
	private void getSingleMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		int mid = Integer.parseInt(request.getParameter("mid"));
		MessageService ms = new MessageServiceImpl();
		Message message = new Message();
		message = ms.getSingleMessage(mid);
		Map<String,Message> map = new HashedMap<>();
		map.put("message",message);
		if(message!=null) {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Message>>("根据mid获取单个Message成功", true, map)));
		}else {
			out.print(new Gson().toJson(new BaseDataPojo<Map<String, 
					Message>>("根据mid获取单个Message为空或失败", false, map)));
		}
		
	}

}



/*
quu..__
 $$$b  `---.__
  "$$b        `--.                          ___.---uuudP
   `$$b           `.__.------.__     __.---'      $$$$"              .
     "$b          -'            `-.-'            $$$"              .'|
       ".                                       d$"             _.'  |
         `.   /                              ..."             .'     |
           `./                           ..::-'            _.'       |
            /                         .:::-'            .-'         .'
           :                          ::''\          _.'            |
          .' .-.             .-.           `.      .'               |
          : /'$$|           .@"$\           `.   .'              _.-'
         .'|$u$$|          |$$,$$|           |  <            _.-'
         | `:$$:'          :$$$$$:           `.  `.       .-'
         :                  `"--'             |    `-.     \
        :##.       ==             .###.       `.      `.    `\
        |##:                      :###:        |        >     >
        |#'     `..'`..'          `###'        x:      /     /
         \                                   xXX|     /    ./
          \                                xXXX'|    /   ./
          /`-.                                  `.  /   /
         :    `-  ...........,                   | /  .'
         |         ``:::::::'       .            |<    `.
         |             ```          |           x| \ `.:``.
         |                         .'    /'   xXX|  `:`M`M':.
         |    |                    ;    /:' xXXX'|  -'MMMMM:'
         `.  .'                   :    /:'       |-'MMMM.-'
          |  |                   .'   /'        .'MMM.-'
          `'`'                   :  ,'          |MMM<
            |                     `'            |tbap\
             \                                  :MM.-'
              \                 |              .''
               \.               `.            /
                /     .:::::::.. :           /
               |     .:::::::::::`.         /
               |   .:::------------\       /
              /   .''               >::'  /
              `',:                 :    .'
                                   `:.:'

 
*/
